﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Net;
using System.IO;
using Clipping.Utils;

namespace ClippingEditor
{
    /// <summary>
    /// Interaction logic for UploadProgress.xaml
    /// </summary>
    public partial class UploadProgress : Window
    {
       
        private string fileName;
        private string serverUrl;
        private string userName;
        private string password;
        private WebClient wc;
        private Log4cb.ILog4cbHelper logHelper;
        public UploadProgress()
        {
            InitializeComponent();

        }

        public UploadProgress(string title,string fileName, string serverUrl, string userName, string password, Log4cb.ILog4cbHelper logHelper)
            : this()
        {
   
            this.tbx_Title.Text = string.Format("正在上传剪报：{0}",title);
            this.fileName = fileName;
            this.serverUrl = serverUrl;
            this.userName = userName;
            this.password = password;
            this.logHelper = logHelper;
            this.Loaded += new RoutedEventHandler(UploadProgress_Loaded);
        }

        void UploadProgress_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                wc = new WebClient();
                wc.UploadFileCompleted += new UploadFileCompletedEventHandler(wc_UploadFileCompleted);
                wc.UploadProgressChanged += new UploadProgressChangedEventHandler(wc_UploadProgressChanged);
                if (this.serverUrl.StartsWith("http", StringComparison.OrdinalIgnoreCase))
                {
                    string uploadurl = string.Format("{0}?user={1}&pwd={2}", this.serverUrl, this.userName, this.password);
                    wc.Credentials = new NetworkCredential(this.userName, this.password);
                    wc.UploadFileAsync(new Uri(uploadurl), fileName);
                }
                else if (this.serverUrl.StartsWith("ftp", StringComparison.OrdinalIgnoreCase))
                {
                    FileInfo fi = new FileInfo(fileName);
                    string uploadurl = string.Format("{0}/{1}", this.serverUrl, fi.Name);
                    wc.Credentials = new NetworkCredential(this.userName, this.password);
                    wc.UploadFileAsync(new Uri(uploadurl), fileName);
                }
                else { this.Close(); }
            }
            catch (Exception ex)
            {
                this.logHelper.LogErrMsg(ex, "上传剪报失败");
            }
        }

        void wc_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
        {
            DispatcherHelper.InvokeAction(() => { this.pb.Value = e.ProgressPercentage; });
        }

        void wc_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
        {
            try
            {
                
                if (!e.Cancelled && e.Error == null)
                    this.DialogResult = true;
                else
                    this.DialogResult = false;
                wc.Dispose();
            }
            catch { }
            finally
            {
                this.Close();
            }
        }

        private void btn_Cancel_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                wc.CancelAsync();
                wc.Dispose();
            }
            catch { }
            finally
            {
                this.DialogResult = false;
                this.Close();
            }
        }



    }
}
